﻿using System;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace Tavis.Http.Tests.Fakes {

    public class FakeWebRequestChannel : HttpMessageHandler {
        private readonly HttpResponseMessage _ResponseMessage;
        private  HttpContent _Content;

        public FakeWebRequestChannel(HttpContent content)  {
            Content = content;
        }
        public FakeWebRequestChannel(HttpResponseMessage responseMessage) {
            _ResponseMessage = responseMessage;
        }

        public HttpContent Content {
            get { return _Content; }
            set { _Content = value; }
        }

        private HttpResponseMessage Send(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) {
            if (_ResponseMessage != null) return _ResponseMessage;

            var response = new HttpResponseMessage() {
                Content = Content,
                RequestMessage = request,
                StatusCode = HttpStatusCode.OK
            };
            return response;
        }


		protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
		{
			return Task.Factory.StartNew(() => Send(request, cancellationToken));
		}
    }
}
